/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.view;

import javax.swing.JDialog;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;

import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import java.awt.Dimension;

/**
 * <p>A user interface component to display a dialog which contains
 * information about the ServiceHub application.</p>
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public class AboutDialog extends JDialog implements ActionListener {

	/**
	 * Create a new object of class AboutDialog
	 * @throws HeadlessException
	 */
	public AboutDialog(Frame owner) throws HeadlessException {
		super(owner);

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		JLabel lb;
		Container contentPane = getContentPane();

		this.setIconImage(Toolkit.getDefaultToolkit().createImage(
				this.getClass().getResource("resources/appIcon_sml.gif")));
		setTitle("About this application");
		setSize(600, 400);

		contentPane.setLayout(new BorderLayout());

		// create header text panel
		JPanel headerPanel = new JPanel(new BorderLayout());
		headerPanel.setBackground(Color.WHITE);

		// add about phrase
		lb = new JLabel("Info");
		Font f = lb.getFont();
		Font boldFont = new Font(f.getName(), Font.BOLD, f.getSize());
		lb.setFont(boldFont);
		lb.setBorder(new EmptyBorder(0, 5, 0, 0));
		headerPanel.add(lb, BorderLayout.NORTH);

		// add header/explanation phrases
		lb = new JLabel("<html><p>" + "You can click on the tabs to see information" +
				"</p><p>" +
				"about version and license of this application." +
		"</p></html>");
		lb.setBorder(new EmptyBorder(0, 15, 0, 0));
		headerPanel.add(lb, BorderLayout.CENTER);

		// get icon
		lb = new JLabel();
    //lb.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(
		//		this.getClass().getResource("resources/appIcon.gif"))));
		lb.setIcon(new ImageIcon(getClass().getResource("resources/appIcon.gif")));

		// create north panel
		JPanel northPanel = new JPanel(new BorderLayout());
		northPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		northPanel.setBackground(Color.WHITE);
		northPanel.add(headerPanel, BorderLayout.CENTER);
		northPanel.add(lb, BorderLayout.EAST);

		// assemble application info text
		StringBuffer buf = new StringBuffer();
		buf.append("<html><p>");
		buf.append("<b>");
		buf.append("ServiceHub");
		buf.append("</b><br>");
		buf.append("Bringing web technology to the desktop.<br><br>");
		buf.append("Version 1.0 r95, March 10, 2010<br>");
		buf.append("Copyright (c) Ulrich Hilger, http://dev.uhilger.de<br><br><br><br>");
		//buf.append("<br><br><br>");
		buf.append("You can use the application help for additional information. ");
		buf.append("Select 'Close' and press F1 to display help.");
		buf.append("</p></html>");

		// create the application info panel
		JPanel appPanel = new JPanel(new BorderLayout());
		appPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
		lb = new JLabel(buf.toString());
		appPanel.add(lb, BorderLayout.NORTH);

		// create the application license panel
		JPanel licPanel = new JPanel(new BorderLayout(5, 5));
		licPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		lb = new JLabel("<html><p>" +
				"This application may be used as long as the following conditions are met" +
		"</p></html>");
		licPanel.add(lb, BorderLayout.NORTH);
		licPanel.add(new LicensePane(new Dimension(500, 200), LICENSE), BorderLayout.CENTER);

		// create the tabbed pane
		JTabbedPane tp = new JTabbedPane();
		tp.setBorder(new EmptyBorder(5, 5, 0, 5));
		tp.addTab("Application", appPanel);
		tp.addTab("License", licPanel);

		// create button panel
		JPanel btnPanel = new JPanel(new BorderLayout());
		btnPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		okBtn = new JButton("Close");
		okBtn.addActionListener(this);
		btnPanel.add(okBtn, BorderLayout.EAST);

		// add all main parts to the content pane
		contentPane.add(northPanel, BorderLayout.NORTH);
		contentPane.add(tp, BorderLayout.CENTER);
		contentPane.add(btnPanel, BorderLayout.SOUTH);
	}

	/**
	 * handle button actions for the close button
	 * @param e ActionEvent
	 */
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source.equals(okBtn)) {
			dispose();
		}
	}

	/** the close button */
	private JButton okBtn;

	/** name of the license file */
	private String LICENSE = "resources/lic.txt";
}
